package org.smart4j.chapter1.service;

import org.smart4j.chapter1.annotation.Transaction;
import org.smart4j.chapter1.bean.FileParam;
import org.smart4j.chapter1.helper.DatabaseHelper;
import org.smart4j.chapter1.helper.UploadHelper;
import org.smart4j.chapter1.model.Customer;
import org.smart4j.chapter1.util.PropsUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * description: 提供客户数据服务
 * Created by gy on 2017/4/19.
 */
public class CustomerService {


    /**
     * 获取客户列表
     *
     * @return
     */
    public List<Customer> getCustomerList() {
        String sql = "SELECT * FROM customer";
        return DatabaseHelper.queryEntityList(Customer.class, sql);
    }

    /**
     * 获取客户
     *
     * @param id
     * @return
     */
    public Customer getCustomer(long id) {
        String sql = "SELECT * FROM customer WHERE id = ?";
        return DatabaseHelper.queryEntity(Customer.class, sql, id);
    }

    /**
     * 创建客户
     *
     * @param filedMap
     * @return
     */
    @Transaction
    public boolean createCustomer(Map<String, Object> filedMap, FileParam fileParam) {

        Boolean result = DatabaseHelper.insertEntity(Customer.class, filedMap);
        if(result){
            UploadHelper.uploadFile("/tmp/upload",fileParam);
        }

        return result;
    }

    /**
     * 更新客户
     *
     * @param id
     * @param fieleMap
     * @return
     */
    @Transaction
    public boolean updateCustomer(long id, Map<String, Object> fieleMap) {
        return DatabaseHelper.updateEntity(Customer.class,id,fieleMap);
    }

    /**
     * 删除客户
     *
     * @param id
     * @return
     */
    @Transaction
    public boolean deleteCustomer(long id) {
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }
}
